import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class MemberCashOutAccountService extends BaseService<any> {
  private readonly logger = new Logger(MemberCashOutAccountService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: MemberCashOutAccountService_api::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $where['member_id'] = $this->member_id;         $where['site_id'] = $this->site_id;         $field = 'account_id,site_id,member_id,account_type,bank_n...
where.member_id = this.member_id;
        where.site_id = this.site_id;
        const field = 'account_id,site_id,member_id,account_type,bank_name,realname,account_no';
        const search_model = this.model.where(where).field(field).order('create_time desc'];

        return this.pageQuery(search_model);
    }

    /**
     * 提现账户详情
     * @param int account_id
     * @return */
    async getInfo(int account_id)
    {
        const field = 'account_id,site_id,member_id,account_type,bank_name,realname,account_no, transfer_payment_code';
        return this.model.where([['account_id', '=', account_id], ['site_id', '=', this.site_id], ['member_id', '=', this.member_id))).field(field).findOrEmpty().toArray(];
    }

    /**
     * 获取首条信息
     * @param where
     * @param string order_field
     * @param string order
     * @return */
    async getFirstInfo(where, order_field = 'create_time', string order = 'desc'){
        where[] = ['site_id', '=', this.site_id];
        where[] = ['member_id', '=', this.member_id];
        const field = 'account_id,site_id,member_id,account_type,bank_name,realname,account_no';
        return this.model.where(where).order(order_field, order).field(field).findOrEmpty().toArray(];
    }

    /**
     * 添加提现账号
     * @param data
     * @return  int
     */
    async add(data)
    {
        data.site_id = this.site_id;
        data.member_id = this.member_id;
        data.create_time = time(];
        const res = this.model.create(data);
        return res.account_id;
    }

    /**
     * 修改提现账户
     * @param int account_id
     * @param data
     * @return true
     */
    async edit(int account_id, data)
    {
        data.update_time = time(];
        this.model.update(data, [ [ 'site_id', '=', this.site_id ], [ 'member_id', '=', this.member_id ], ['account_id', '=', account_id) )];
        return true;
    }

    /**
     * 删除
     * @param int account_id
     * @return true
     */
    async del(int account_id)
    {
        const where = [
            ['member_id', '=', this.member_id],
            ['site_id', '=', this.site_id],
            ['account_id', '=', account_id]
        ];
        this.model.where(where).delete(];
        return true;
    }
}
  }

  /**
   * getInfo
   * 对应 PHP: MemberCashOutAccountService_api::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(account_id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'account_id,site_id,member_id,account_type,bank_name,realname,account_no, transfer_payment_code';         return $this->model->where([['accou...
const field = 'account_id,site_id,member_id,account_type,bank_name,realname,account_no, transfer_payment_code';
        return this.model.where([['account_id', '=', account_id], ['site_id', '=', this.site_id], ['member_id', '=', this.member_id))).field(field).findOrEmpty().toArray(];
    }

    /**
     * 获取首条信息
     * @param where
     * @param string order_field
     * @param string order
     * @return */
    async getFirstInfo(where, order_field = 'create_time', string order = 'desc'){
        where[] = ['site_id', '=', this.site_id];
        where[] = ['member_id', '=', this.member_id];
        const field = 'account_id,site_id,member_id,account_type,bank_name,realname,account_no';
        return this.model.where(where).order(order_field, order).field(field).findOrEmpty().toArray(];
    }

    /**
     * 添加提现账号
     * @param data
     * @return  int
     */
    async add(data)
    {
        data.site_id = this.site_id;
        data.member_id = this.member_id;
        data.create_time = time(];
        const res = this.model.create(data);
        return res.account_id;
    }

    /**
     * 修改提现账户
     * @param int account_id
     * @param data
     * @return true
     */
    async edit(int account_id, data)
    {
        data.update_time = time(];
        this.model.update(data, [ [ 'site_id', '=', this.site_id ], [ 'member_id', '=', this.member_id ], ['account_id', '=', account_id) )];
        return true;
    }

    /**
     * 删除
     * @param int account_id
     * @return true
     */
    async del(int account_id)
    {
        const where = [
            ['member_id', '=', this.member_id],
            ['site_id', '=', this.site_id],
            ['account_id', '=', account_id]
        ];
        this.model.where(where).delete(];
        return true;
    }
}
  }

  /**
   * getFirstInfo
   * 对应 PHP: MemberCashOutAccountService_api::getFirstInfo()
   * 逻辑类型: undefined - undefined
   */
  async getFirstInfo(where: any[], order_field: any[], order: any[]) {
    // 基于PHP真实逻辑: getFirstInfo
    // PHP原文: $where[] = ['site_id', '=', $this->site_id];         $where[] = ['member_id', '=', $this->member_id];         $field = 'account_id,site_id,member_id,a...
where[] = ['site_id', '=', this.site_id];
        where[] = ['member_id', '=', this.member_id];
        const field = 'account_id,site_id,member_id,account_type,bank_name,realname,account_no';
        return this.model.where(where).order(order_field, order).field(field).findOrEmpty().toArray(];
    }

    /**
     * 添加提现账号
     * @param data
     * @return  int
     */
    async add(data)
    {
        data.site_id = this.site_id;
        data.member_id = this.member_id;
        data.create_time = time(];
        const res = this.model.create(data);
        return res.account_id;
    }

    /**
     * 修改提现账户
     * @param int account_id
     * @param data
     * @return true
     */
    async edit(int account_id, data)
    {
        data.update_time = time(];
        this.model.update(data, [ [ 'site_id', '=', this.site_id ], [ 'member_id', '=', this.member_id ], ['account_id', '=', account_id) )];
        return true;
    }

    /**
     * 删除
     * @param int account_id
     * @return true
     */
    async del(int account_id)
    {
        const where = [
            ['member_id', '=', this.member_id],
            ['site_id', '=', this.site_id],
            ['account_id', '=', account_id]
        ];
        this.model.where(where).delete(];
        return true;
    }
}
  }

  /**
   * add
   * 对应 PHP: MemberCashOutAccountService_api::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: $data['site_id'] = $this->site_id;         $data['member_id'] = $this->member_id;         $data['create_time'] = time();         $res = $this->model->...
data.site_id = this.site_id;
        data.member_id = this.member_id;
        data.create_time = time(];
        const res = this.model.create(data);
        return res.account_id;
    }

    /**
     * 修改提现账户
     * @param int account_id
     * @param data
     * @return true
     */
    async edit(int account_id, data)
    {
        data.update_time = time(];
        this.model.update(data, [ [ 'site_id', '=', this.site_id ], [ 'member_id', '=', this.member_id ], ['account_id', '=', account_id) )];
        return true;
    }

    /**
     * 删除
     * @param int account_id
     * @return true
     */
    async del(int account_id)
    {
        const where = [
            ['member_id', '=', this.member_id],
            ['site_id', '=', this.site_id],
            ['account_id', '=', account_id]
        ];
        this.model.where(where).delete(];
        return true;
    }
}
  }

  /**
   * edit
   * 对应 PHP: MemberCashOutAccountService_api::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(account_id: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: $data['update_time'] = time();         $this->model->update($data, [ [ 'site_id', '=', $this->site_id ], [ 'member_id', '=', $this->member_id ], ['acc...
data.update_time = time(];
        this.model.update(data, [ [ 'site_id', '=', this.site_id ], [ 'member_id', '=', this.member_id ], ['account_id', '=', account_id) )];
        return true;
    }

    /**
     * 删除
     * @param int account_id
     * @return true
     */
    async del(int account_id)
    {
        const where = [
            ['member_id', '=', this.member_id],
            ['site_id', '=', this.site_id],
            ['account_id', '=', account_id]
        ];
        this.model.where(where).delete(];
        return true;
    }
}
  }

  /**
   * del
   * 对应 PHP: MemberCashOutAccountService_api::del()
   * 逻辑类型: undefined - undefined
   */
  async del(account_id: number) {
    // 基于PHP真实逻辑: del
    // PHP原文: $where = [             ['member_id', '=', $this->member_id],             ['site_id', '=', $this->site_id],             ['account_id', '=', $account_id...
const where = [
            ['member_id', '=', this.member_id],
            ['site_id', '=', this.site_id],
            ['account_id', '=', account_id]
        ];
        this.model.where(where).delete(];
        return true;
    }
}
  }
}
